< previous page page_364 next page >

Page 364
The cmdStruct1_Click function can be used to examine this structure both as it is stored within Visual Basic and as it is passed during a DLL function call. The 'a' and 'b' fields of the structure are set to hex values &H1234 and &H5678, respectively. These numbers are arbitrary and will be easily recognized. Both strings are set to "ABCD" as follows:
Private Sub cmdStruct1_Click()
   Dim s As Struct1
   Dim b(40) AS Byte
   s.a = &H1234
   s.b = &H5678
   s.c = "ABCD"
   s.d = "ABCD"
   Debug.Print "Structure contains on API call: "
   RtlMoveMemory b(0), s, Len(s)
   ShowMemory VarPtr(b(0)), Len(s)
   ' Why can't we look at the string data?
   ' Answer - temporary buffers!
   Debug.Print "In VB it contains: "
   ShowMemory VarPtr(s), LenB(s)
End Sub
The only reliable way to see what a structure looks like during an API call is to make an API call. In this case, we call the ever-useful RtlMoveMemory function. The destination parameter is the first byte of a byte array. The source parameter is the structure. Visual Basic will pass a pointer to that structure. The RtlMoveMemory function will copy the structure data into the byte array se we can display its contents. The resulting display is as follows:
Structure contains on API call:
17B168: 34 12 78 56 41 42 43 44 F4 2E 18 0
The first two bytes are &H34 and &H12. As you may recall from Tutorial 2, "Memory, Where It All Begins," this corresponds to the 16-bit value &H1234. The bytes only appear swapped because the lower byte of the value appears in lower memory, which is displayed first in the memory buffer dump.
The second integer is &H5678 and appears immediately after the first.
Next, you can see four character, &H41, &H42, &H43, and &H44. These are the ASCII codes for the letters "ABCD"the fixed string variable 'c'. The final four bytes are the pointer &H182EF4 (which almost certainly will have a different value on your system). This is a BSTR pointer to an ANSI string created temporarily during the calling process. We'll talk more about dynamic strings inside structures later.

 
< previous page page_364 next page >